<?php
class Subject extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model(array(
						'Task_m'=>'task',
						'Subject_m'=>'subject',
						'Major_m'=>'major'));
		$this->load->library('result');
	}
	
	function getsubjects(){
		$type=$this->uri->segment(3,-1);
		if($type==-1){
			$data=$this->subject->query(null);
		}else{
			$data=$this->subject->query(array('majorId'=>$type));
		}
		if(!empty($data)){
			$this->result->setData(0,'获取数据完成',$data);
		}else{
			$this->result->setData(1,'获取数据失败',null);
		}
		$this->result->output();
	}

	function getsubjects2(){
		$name=urldecode($this->uri->segment(3,""));
		$data=$this->subject->query2(array('name'=>$name));
		if(!empty($data)){
			$this->result->setData(0,'获取数据完成',$data);
		}else{
			$this->result->setData(1,'获取数据失败',null);
		}
		$this->result->output();
	}
	
	function gettypes(){
		$data=$this->major->query();
		if(!empty($data)){
			$this->result->setData(0,'获取数据完成',$data);
		}else{
			$this->result->setData(1,'获取数据失败',null);
		}
		$this->result->output();
	}
	
	function getfavoritesubjects(){
		$uid=$this->input->post('uid');
		//$uid=$this->uri->segment(3,-1);
		
		$data=$this->subject->querybyuser($uid);
		if(!empty($data)){
			$this->result->setData(0,'获取数据完成',$data);
		}else{
			$this->result->setData(1,'获取数据失败',null);
		}
		$this->result->output();
	}
	
	function getsubjectinfo(){
		$sid=$this->uri->segment(3,-1);
		$subdata=$this->subject->query(array('id'=>$sid));
		$subtask=$this->task->query(array('subjectid'=>$sid));
		$data=array(
			'info'=>$subdata,
			'task'=>$subtask
		);
		if(empty($subdata)||empty($subtask)){
			$this->result->setData(1,'获取数据失败',null);
		}else{
			$this->result->setData(0,'获取数据完成',$data);
		}
		$this->result->output();
	}
}